package com.hzf.common.face;

import com.google.gson.Gson;
import com.hzf.common.face.faceModel.FaceEntity;
import com.hzf.common.face.faceModel.Thresholds;
import com.hzf.common.util.HttpClientUtil;
import com.hzf.common.util.PropertiesFileUtil;
import net.sf.json.JSONObject;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FaceUtils {
     public static String  httpOrgCreateUrl="https://api-cn.faceplusplus.com/facepp/v3/compare";
     public static String OCRIDCardAPIUrl = "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard";
    public static FaceEntity httpPost(FaceModel faceModel){
        String httpOrgCreateTest = "https://api-cn.faceplusplus.com/facepp/v3/compare";
        Map<String,String> createMap = new HashMap<String,String>();
        createMap.put("api_key",faceModel.getApi_key());
        createMap.put("api_secret",faceModel.getApi_secret());
        createMap.put("image_url1",faceModel.getImage_url1());
        createMap.put("image_url2",faceModel.getImage_url2());
        String httpOrgCreateTestRtn = HttpClientUtil.doPost(httpOrgCreateTest,createMap,"utf-8");
        FaceEntity faceEntity =new Gson().fromJson(httpOrgCreateTestRtn,FaceEntity.class);
        return  faceEntity;
    }

    public static JSONObject OCRIDCardAPI(FaceCard faceCard){
        String httpOrgCreateTest = OCRIDCardAPIUrl;
        Map<String,String> createMap = new HashMap<String,String>();
        createMap.put("api_key", PropertiesFileUtil.getInstance("hzf-face").get("face.apikey"));
        createMap.put("api_secret", PropertiesFileUtil.getInstance("hzf-face").get("face.apiSecret"));
        createMap.put("image_url",faceCard.getImage_url());
        createMap.put("legality","1");
        String httpOrgCreateTestRtn = HttpClientUtil.doPost(httpOrgCreateTest,createMap,"utf-8");
        String s = replaceBlank(httpOrgCreateTestRtn);
        System.out.println(s);
        return  JSONObject.fromObject(s);
    }
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
